﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using HotelManagement.Common;
using HotelManagement.BL;

namespace HotelManagement.PL.FunctionControl
{
	public partial class TradeMngUserControl : BaseUserControl
    {
        #region Contructor
        public TradeMngUserControl()
        {
            InitializeComponent();
            UpdateGridView();
        }
        #endregion

        #region Event Handler
		/// <summary>
		/// btnSearch's click event handler
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
        private void btnSearch_Click(object sender, EventArgs e)
        {
            UpdateGridView();			
        }

		/// <summary>
		///  btnPurchase's click event handler
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
        private void btnPurchase_Click(object sender, EventArgs e)
        {
            int nSelectRow = CheckChoose();
            if (nSelectRow < 0)
            {
                return;
            }
            TradeForm purchaseForm = new TradeForm(TradeType.Purchase);
            purchaseForm.GoodInfo = buildGoods(nSelectRow);
            purchaseForm.ShowDialog(this);
			if (purchaseForm.DialogResult == DialogResult.OK)
			{
				UpdateGridView();				
			}
        }

		/// <summary>
		/// benSale's click event handler
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
        private void btnSale_Click(object sender, EventArgs e)
        {
            int nSelectRow = CheckChoose();
            if (nSelectRow < 0)
            {
                return;
            }
            TradeForm saleForm = new TradeForm(TradeType.Sell);
            saleForm.GoodInfo = buildGoods(nSelectRow);
            saleForm.ShowDialog(this.ParentForm);
			if (saleForm.DialogResult == DialogResult.OK)
			{
				UpdateGridView();
			}
        }
        #endregion

        #region Private Method
		/// <summary>
		/// update gridview
		/// </summary>
        internal void UpdateGridView()
        {
            int nResult;
            try
            {
                Goods goods = new Goods();
                goods.Name = txtName.Text.Trim();

                GoodsBL searchBL = new GoodsBL();
                nResult = searchBL.Search(ref goods);

                if (goods.GoodsCollection != null)
                {
                    dgvGoods.AutoGenerateColumns = false;
                    dgvGoods.DataSource = goods.GoodsCollection;
                }

				OnUpdateGoodsStatus();
            }
            catch
            {

            }
        }

		/// <summary>
		/// check whether gridview's checkbox is checked
		/// </summary>
		/// <returns></returns>
		private int CheckChoose()
		{
			int nCount = 0;
			int nRowIndex = -1;
			foreach (DataGridViewRow dgr in dgvGoods.Rows)
			{
				if (dgr.Cells["choose"].Value != null && dgr.Cells["choose"].Value.Equals(true))
				{
					nCount++;
					nRowIndex = dgr.Index;
				}
			}

			if (nCount == 0)
			{
				MessageBox.Show("请选择货物", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
				nRowIndex = -1;
			}

			if (nCount > 1)
			{
				MessageBox.Show("只能选择一个", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
				nRowIndex = -1;
			}

			return nRowIndex;
		}

		/// <summary>
		/// build goods datamodel by the data from gridview
		/// </summary>
		/// <param name="rowIndex"></param>
		/// <returns></returns>
		private Goods buildGoods(int rowIndex)
		{
			Goods goods = new Goods();
			goods.ID = Convert.ToInt32(dgvGoods.Rows[rowIndex].Cells["ID"].Value.ToString());
			goods.Name = dgvGoods.Rows[rowIndex].Cells["NAME"].Value.ToString();
			goods.Count = Convert.ToInt32(dgvGoods.Rows[rowIndex].Cells["COUNT"].Value);
			goods.PurchasePrice = Convert.ToSingle(dgvGoods.Rows[rowIndex].Cells["PURCHASEPRICE"].Value);
			goods.SalePrice = Convert.ToSingle(dgvGoods.Rows[rowIndex].Cells["SALEPRICE"].Value);
			goods.WaringCount = Convert.ToInt32(dgvGoods.Rows[rowIndex].Cells["WARINGCOUNT"].Value);
			goods.Description = dgvGoods.Rows[rowIndex].Cells["DESCRIPTION"].Value.ToString();
			return goods;
		}
        #endregion   
    }
}
